package com.tianzeculture.stagebao.activity.home.fragment;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.StyleSelectActivity;
import com.tianzeculture.stagebao.activity.home.SearchActivity;
import com.tianzeculture.stagebao.activity.home.SelectCityActivity;
import com.tianzeculture.stagebao.activity.home.adapter.BestPushAdapter;
import com.tianzeculture.stagebao.activity.home.adapter.MultiplePagerAdapter;
import com.tianzeculture.stagebao.activity.message.SystemMessageActivity;
import com.tianzeculture.stagebao.base.BaseFragment;
import com.tianzeculture.stagebao.bean.ArtistsBean;
import com.tianzeculture.stagebao.bean.BannerBean;
import com.tianzeculture.stagebao.bean.HomeBean;
import com.tianzeculture.stagebao.bean.ImageBean;
import com.tianzeculture.stagebao.bean.VideoBean;
import com.tianzeculture.stagebao.bean.WorkTag;
import com.tianzeculture.stagebao.net.HomeRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.util.Util;
import com.tianzeculture.stagebao.video.PlayerActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/5/28 0028.
 */

public class HomeFragment extends BaseFragment {

    @BindView(R.id.tvLocation)
    TextView tvLocation;
    @BindView(R.id.edtSearch)
    EditText edtSearch;
//    @BindView(R.id.ibtnSystemNews)
//    ImageButton ibtnSystemNews;

    @BindView(R.id.adLayout)
    RelativeLayout adLayout;
    @BindView(R.id.vpAd)
    ViewPager vpAd;
    MultiplePagerAdapter pagerAdapter;

    @BindView(R.id.videoLayout)
    LinearLayout videoLayout;

    @BindView(R.id.rvBestPush)
    RecyclerView rvBestPush;
    BestPushAdapter bestPushAdapter = null;
    @Request
    HomeRestUsage homeRestUsage = null;
    String searchString = "";

    String city = "杭州市";
    private int TO_INDEX = 101;

    @Override
    public void initUI() {
        super.initUI();
        EventBus.getDefault().register(this);
        edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    searchString = edtSearch.getText().toString().trim();
                    showProgressDialog();
                    homeRestUsage.toIndex(TO_INDEX, city, searchString);
                }
                return false;
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        showProgressDialog();
        homeRestUsage.toIndex(TO_INDEX, city, searchString);
    }

    /**
     * 短视频
     */
    private void initVideos(final List<ImageBean> imageBeen) {
        videoLayout.removeAllViews();
        for (int i = 0; i < imageBeen.size(); i++) {
            View view = CardView.inflate(getContext(), R.layout.layout_short_video_item, null);
            ImageView ivVideoImage = view.findViewById(R.id.ivVideoImage);
            ImageView ivPlay = view.findViewById(R.id.ivPlay);
            TextView tvArtists = view.findViewById(R.id.tvArtists);
            Glide.with(getContext()).load(PICTURE_URL + imageBeen.get(i).getIndexPath()).into(ivVideoImage);
            tvArtists.setText(imageBeen.get(i).getUser().getNickname());
            final int finalI = i;
            ivPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent_play = new Intent(getContext(), PlayerActivity.class);
                    if (!TextUtils.isEmpty(imageBeen.get(finalI).getPath()))
                        intent_play.putExtra("path", imageBeen.get(finalI));
//                        intent_play.putExtra("path", PICTURE_URL + imageBeen.get(finalI).getPath());
                    startActivity(intent_play);
//                    intent.putExtra("url", videoBeans.get(finalI).getVideoUrl());
//                    startActivity(intent);
                }
            });
            videoLayout.addView(view);
        }
    }

    /**
     * 推荐艺人
     */
    private void initBestPush(List<ImageBean> imageBeen) {
        rvBestPush.setNestedScrollingEnabled(false); //解决滑动不流畅
//        rvBestPush.addItemDecoration(new Util.SpaceItemDecoration(10, 15));
//        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager
                (2, StaggeredGridLayoutManager.VERTICAL);
        rvBestPush.setLayoutManager(layoutManager);
        rvBestPush.setHasFixedSize(true);
        bestPushAdapter = new BestPushAdapter(getContext(), imageBeen);
        rvBestPush.setAdapter(bestPushAdapter);
    }


    @OnClick(R.id.tvLocation)
    public void toSelectCity(View view) {
        startActivity(new Intent(getContext(), SelectCityActivity.class));
    }

    @OnClick(R.id.edtSearch)
    public void toSearch(View view) {
        startActivity(new Intent(getContext(), SearchActivity.class));
    }

    @OnClick(R.id.tvSinger)
    public void tvSinger(View view) {
        startActivity(new Intent(getContext(), StyleSelectActivity.class).putExtra("style", "歌手"));
    }

    @OnClick(R.id.tvModel)
    public void tvModel(View view) {
        startActivity(new Intent(getContext(), StyleSelectActivity.class).putExtra("style", "模特"));
    }

    @OnClick(R.id.tvHost)
    public void tvHost(View view) {
        startActivity(new Intent(getContext(), StyleSelectActivity.class).putExtra("style", "主持"));
    }

    @OnClick(R.id.tvSelebrity)
    public void tvSelebrity(View view) {
        startActivity(new Intent(getContext(), StyleSelectActivity.class).putExtra("style", "网红"));
    }

    @OnClick(R.id.tvOther)
    public void tvOther(View view) {
        startActivity(new Intent(getContext(), StyleSelectActivity.class).putExtra("style", "其他"));
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == TO_INDEX) {
            if (msg.getData()) {
                HomeBean homeBean = (HomeBean) msg.getObj();
                List<BannerBean> bannerlist = homeBean.getBannerlist();
                if (bannerlist != null)
                    if (bannerlist.size() > 0) {
                        pagerAdapter = new MultiplePagerAdapter(getContext(), bannerlist);
                        vpAd.setAdapter(pagerAdapter);
                    }
                List<ImageBean> videolist = homeBean.getWorkervideo();
                if (videolist != null)
                    if (videolist.size() > 0)
                        initVideos(videolist);
                List<ImageBean> bestPushlist = homeBean.getWorkprc();
                if (bestPushlist != null)
                    if (bestPushlist.size() > 0)
                        initBestPush(bestPushlist);
            }
            dismissProgressDialog();
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getCity(WorkTag tag) {
        if (tag != null) {
            city = tag.getName();
            tvLocation.setText(city);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_home;
    }
}
